import axios from 'axios'
import evn from './evn'
import { Message } from 'element-ui'
const instans = axios.create({
  baseURL: evn.xianShang.baseURL,
  timeout: 5000
})
instans.interceptors.request.use(
  (config) => {
    // console.log('请求拦截器::', config)
    if (config.url !== '/login') {
      const token = sessionStorage.getItem('token')
      token && (config.headers.Authorization = token)
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)
instans.interceptors.response.use(
  (res) => {
    const {
      meta: { status, msg }
    } = res.data
    // console.log(status)
    // console.log(msg)
    const Arr = [200, 201, 204]
    if (Arr.includes(status)) {
      Message({
        type: 'success',
        message: msg
      })
      if (res.config.url === '/login') {
     sessionStorage.setItem('token', res.data.data.token)
      }
      return res.data.data
    }
    // console.log('响应拦截器::', res)
    Message({
      type: 'error',
      message: msg
    })

    return Promise.reject(msg)
  },
  (error) => {
    return Promise.reject(error)
  }
)

export default instans
